<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996 15899929162
// +----------------------------------------------------------------------
namespace app\common\dao\energy;

use app\common\dao\BaseDao;
use app\common\model\energy\EnergyMeterModel;

class EnergyMeterDao extends BaseDao
{

    /**
     * @return EnergyMeterModel
     */
    protected function getModel(): string
    {
        return EnergyMeterModel::class;
    }


    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['is_bind']) && $where['is_bind'] !== '', function ($query) use ($where) {
                $query->where('is_bind', intval($where['is_bind']));
            })
            ->when(isset($where['add_uid']) && $where['add_uid'] !== '', function ($query) use ($where) {
                $query->where('add_uid', intval($where['add_uid']));
            })
            ->when(isset($where['is_public']) && $where['is_public'] !== '', function ($query) use ($where) {
                $query->where('is_public', intval($where['is_public']));
            })
            ->when(isset($where['room_id']) && $where['room_id'] !== '', function ($query) use ($where) {
                $query->where('room_id', intval($where['room_id']));
            })
            ->when(isset($where['energy_type']) && $where['energy_type'] !== '', function ($query) use ($where) {
                $query->where('energy_type', intval($where['energy_type']));
            })
            ->when(isset($where['plan_id']) && $where['plan_id'] !== '', function ($query) use ($where) {
                $query->where('plan_id', intval($where['plan_id']));
            })
            ->when(isset($where['plan_type']) && $where['plan_type'] !== '', function ($query) use ($where) {
                if (is_array($where['plan_type'])) {
                    $query->whereIn('plan_type', $where['plan_type']);
                } else {
                    $query->where('plan_type', intval($where['plan_type']));
                }
            })
            ->when(isset($where['firm_id']) && $where['firm_id'] !== '', function ($query) use ($where) {
                $query->where('firm_id', intval($where['firm_id']));
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('energy_no|energy_name|remark', '%' . trim($where['keywords']) . '%');
            });
    }

    /**
     * 根据房间 ID 解绑设备
     * @param int $roomId
     * @return \app\common\model\BaseModel
     *
     * @author Amos
     * 2024-05-18 15:44
     */
    public function unbindRoomIdEnergyMeterId(int $roomId)
    {
        return $this->getSearch([
            'room_id' => $roomId
        ])->update(['room_id' => 0, 'is_bind' => 2]);
    }

    /**
     * 根据物业 ID 解绑设备
     * @param int $roomId
     * @return \app\common\model\BaseModel
     *
     * @author Amos
     * 2024-05-18 15:44
     */
    public function unbindPropertyIdEnergyMeterId(int $propertyId)
    {
        return $this->getSearch([
            'property_id' => $propertyId
        ])->update(['property_id' => 0, 'room_id' => 0, 'is_bind' => 2]);
    }


    /**
     * 检测设备是否存在
     * @param string $deviceNo
     * @param int|null $except
     * @return bool
     * @throws \think\db\exception\DbException
     *
     * @author Amos
     * 2024-05-18 15:44
     */
    public function energyExists(string $energyNo, int $except = null)
    {
        return $this->getSearch([])
                ->where('energy_no', $energyNo)
                ->when($except, function ($query) use ($except) {
                    $query->where('id', '<>', $except);
                })
                ->count() > 0;
    }
}
